package vanjalee.bs.com.view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import net.coderazzi.filters.gui.AutoChoices;
import net.coderazzi.filters.gui.TableFilterHeader;
import net.coderazzi.filters.gui.TableFilterHeader.Position;
import vanjalee.bs.com.control.DataController;
import vanjalee.bs.com.control.model.ItemType;
import vanjalee.bs.com.control.model.Part;
import vanjalee.bs.com.control.model.Weapon;

public class DataDisplay extends JPanel {
	private static final int NUMBER_OF_CELLS_WEAPONS = 13;
	private static final int NUMBER_OF_CELLS_PARTS = 10;

	private DataController dataController = new DataController();
	private List<Weapon> weaponsCloseCombat = dataController
			.getWeaponsCloseCombat();
	private List<Weapon> weaponsRanged = dataController.getWeaponsLongRange();
	private List<Weapon> weaponsCannons = dataController.getWeaponsMainCannon();
	private List<Weapon> weaponsSubguns = dataController.getWeaponsSubGun();

	private List<Part> partsSail = dataController.getPartsSail();
	private List<Part> partsStab = dataController.getPartsStabilizer();
	private List<Part> partsHull = dataController.getPartsHull();
	private List<Part> partsEng = dataController.getPartsEngine();
	private List<Part> partsFigh = dataController.getPartsFigurehead();

	private static final long serialVersionUID = 1L;

	private JTable table;
	private String[] weaponSpecifics = new String[] { "Name", "Level", "Range",
			"MIN", "MAX", "Average", "Weight", "D/W (%)", "Slow", "Limit",
			"Sold at", "Type", "Further Info" };

	private String[] partSpecifics = new String[] { "Name", "Level", "Speed",
			"HP", "Capacity", "Armor", "Weight", "Sold at", "Type",
			"Further Info" };

	private List<Weapon> chosenWeapons;

	private List<Part> chosenParts;

	private Object[][] weaponDatas;

	private Object[][] partDatas;
	private TableFilterHeader filterHeader;
	private JScrollPane scrollPane;

	public DataDisplay() {
		super(new GridLayout(1, 0));
		initializeData(1);
	}

	public DataDisplay(int type) {
		super(new GridLayout(1, 0));
		initializeData(type);
	}

	public void initializeData(int type) {
		if (type == 4) {
			setIfWeaponsChosen(1);
			initializeForWeapons(1);
		} else if (type == 5) {
			setIfPartsChosen(6);
			initializeForParts(6);
		} else if (type < 4) {
			setIfWeaponsChosen(type);
			initializeForWeapons(type);
		} else {
			setIfPartsChosen(type);
			initializeForParts(type);
		}

	}

	private void setIfWeaponsChosen(int type) {
		if (type == 0) {
			chosenWeapons = weaponsCloseCombat;
		} else if (type == 1) {
			chosenWeapons = weaponsCannons;
		} else if (type == 2) {
			chosenWeapons = weaponsSubguns;
		} else {
			chosenWeapons = weaponsRanged;
		}
	}

	private void setIfPartsChosen(int type) {
		if (type == 6) {
			chosenParts = partsEng;
		} else if (type == 7) {
			chosenParts = partsHull;
		} else if (type == 8) {
			chosenParts = partsFigh;
		} else if (type == 9) {
			chosenParts = partsStab;
		} else {
			chosenParts = partsSail;
		}
	}

	public void initializeForWeapons(int type) {
		weaponDatas = new Object[chosenWeapons.size()][NUMBER_OF_CELLS_WEAPONS];
		for (int i = 0; i < chosenWeapons.size(); i++) {
			weaponDatas[i][0] = chosenWeapons.get(i).getName();
			weaponDatas[i][1] = chosenWeapons.get(i).getLevel();
			weaponDatas[i][2] = chosenWeapons.get(i).getRange();
			weaponDatas[i][3] = chosenWeapons.get(i).getMinDmg();
			weaponDatas[i][4] = chosenWeapons.get(i).getMaxDmg();
			weaponDatas[i][5] = chosenWeapons.get(i).getAvgDmg();
			weaponDatas[i][6] = chosenWeapons.get(i).getWeight();
			weaponDatas[i][7] = chosenWeapons.get(i).getDmgWeight();
			weaponDatas[i][8] = chosenWeapons.get(i).getSlowdown();
			weaponDatas[i][9] = chosenWeapons.get(i).getEquipLimit();
			weaponDatas[i][10] = chosenWeapons.get(i).getWhereToGet();
			weaponDatas[i][11] = chosenWeapons.get(i).getItemType();
			weaponDatas[i][12] = chosenWeapons.get(i).getFurtherInfo();
		}

		table = new JTable(weaponDatas, weaponSpecifics) {
			private static final long serialVersionUID = 2485117672771964339L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		table.setModel(new DefaultTableModel(weaponDatas, weaponSpecifics) {
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("rawtypes")
			Class[] types = { String.class, Integer.class, Integer.class,
					Integer.class, Integer.class, Integer.class, Integer.class,
					Integer.class, Integer.class, Integer.class, String.class,
					ItemType.class, String.class };

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return this.types[columnIndex];
			}
		});

		table.getColumnModel().getColumn(0).setPreferredWidth(120);
		table.getColumnModel().getColumn(1).setPreferredWidth(5);
		table.getColumnModel().getColumn(2).setPreferredWidth(5);
		table.getColumnModel().getColumn(3).setPreferredWidth(5);
		table.getColumnModel().getColumn(4).setPreferredWidth(5);
		table.getColumnModel().getColumn(5).setPreferredWidth(5);
		table.getColumnModel().getColumn(6).setPreferredWidth(5);
		table.getColumnModel().getColumn(7).setPreferredWidth(5);
		table.getColumnModel().getColumn(8).setPreferredWidth(5);
		table.getColumnModel().getColumn(9).setPreferredWidth(5);
		table.getColumnModel().getColumn(10).setPreferredWidth(85);
		table.getColumnModel().getColumn(11).setPreferredWidth(25);
		table.getColumnModel().getColumn(12).setPreferredWidth(350);

		finalizeAndUpdateData();
	}

	public void initializeForParts(int type) {
		partDatas = new Object[chosenParts.size()][NUMBER_OF_CELLS_PARTS];
		for (int i = 0; i < chosenParts.size(); i++) {
			partDatas[i][0] = chosenParts.get(i).getName();
			partDatas[i][1] = chosenParts.get(i).getLevel();
			partDatas[i][2] = chosenParts.get(i).getSpeed();
			partDatas[i][3] = chosenParts.get(i).getHp();
			partDatas[i][4] = chosenParts.get(i).getCapacity();
			partDatas[i][5] = chosenParts.get(i).getArmor();
			partDatas[i][6] = chosenParts.get(i).getWeight();
			partDatas[i][7] = chosenParts.get(i).getWhereToGet();
			partDatas[i][8] = chosenParts.get(i).getItemType();
			partDatas[i][9] = chosenParts.get(i).getFurtherInfo();
		}

		table = new JTable(partDatas, partSpecifics) {
			private static final long serialVersionUID = 2485117672771964339L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}

			public Point getToolTipLocation(MouseEvent event) {
				return new Point(0, 0);
			}
		};

		table.setModel(new DefaultTableModel(partDatas, partSpecifics) {
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("rawtypes")
			Class[] types = { String.class, Integer.class, Integer.class,
					Integer.class, Integer.class, Integer.class, Integer.class,
					String.class, ItemType.class, String.class };

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return this.types[columnIndex];
			}
		});

		table.getColumnModel().getColumn(0).setPreferredWidth(120);
		table.getColumnModel().getColumn(1).setPreferredWidth(5);
		table.getColumnModel().getColumn(2).setPreferredWidth(5);
		table.getColumnModel().getColumn(3).setPreferredWidth(5);
		table.getColumnModel().getColumn(4).setPreferredWidth(5);
		table.getColumnModel().getColumn(5).setPreferredWidth(5);
		table.getColumnModel().getColumn(6).setPreferredWidth(5);
		table.getColumnModel().getColumn(7).setPreferredWidth(85);
		table.getColumnModel().getColumn(8).setPreferredWidth(25);
		table.getColumnModel().getColumn(9).setPreferredWidth(350);

		finalizeAndUpdateData();
	}

	public void finalizeAndUpdateData() {
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);		
		filterHeader = new TableFilterHeader(table,
				AutoChoices.ENABLED);
		filterHeader.setPosition(Position.TOP);
		scrollPane = new JScrollPane(table);
		add(scrollPane);
		table.updateUI();
	}

}
